// pages/my/login/login.js
const app = getApp()
const {
  baseURL
} = require('../../../fetch/http')
const api = require('../../../fetch/api')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    baseURL: baseURL,
    show: false,
  },
  async getPhoneNumber(e) {
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      let {
        encryptedData,
        iv
      } = e.detail
      let {
        encryptedData1,
        iv1,
        assistant
      } = this.data
      if (!iv1) {
        app.toast('请先授权登录')
        return
      }
      console.log( wx.getStorageSync('recommend'),"useid")
      wx.login({
  
        success: res => {
          let code = res.code
          api.register({
            code,
            iv1,
            encryptedData1,
            iv2: iv,
            encryptedData2: encryptedData,
            recommend: wx.getStorageSync('recommend'),
            assistant,
          }).then(res => {
           console.log(res);
            wx.setStorageSync('token', res.data.token)
            wx.setStorageSync('id', res.data.mid)
            app.toast('登录成功', 'success', () => {
              let routers = getCurrentPages()
              if(routers[routers.length-2]){
                wx.navigateBack()
              }else {
                wx.switchTab({
                  url: '/index/index',
                })
              }
            })
          })
        }
      })
    }
  },
  async getUserInfo(e) {
    if (e.detail.errMsg == "getUserInfo:ok") {
      const {
        encryptedData,
        iv
      } = e.detail
      this.setData({
        encryptedData1: encryptedData,
        iv1: iv
      })
      const self = this
      wx.login({
        success(res) {
          api.login({
            code: res.code
          }).then(res => {
            if (res.code == 200) {
              wx.setStorageSync('token', res.data.token)
              wx.setStorageSync('id', res.data.mid)
              app.toast('登录成功', 'success', () => {
                let routers = getCurrentPages()
                if(routers[routers.length-2]){
                  wx.navigateBack()
                }else {
                  wx.switchTab({
                    url: '/index/index',
                  })
                }
              })
            } else {
              self.setData({
                show: true
              })
            }
          })
        },
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    console.log(options);
    let {
      scene,id
    } = options
    if(id) {
      wx.setStorageSync('recommend', id)
    }
    if (scene) {
      wx.setStorageSync('recommend', scene)
    }
    if (wx.getStorageSync('token')) {
      wx.switchTab({
        url: '/index/index',
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      recommend: wx.getStorageSync('recommend')
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})